@echo off
setlocal enabledelayedexpansion

echo ==========================================
echo Sokit CLI Linux x86 (32-bit) Build Script
echo ==========================================

echo.
echo This script will build 32-bit Linux version of sokit-cli
echo.

REM Check Docker
where docker >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: Docker not found
    echo Please install Docker Desktop first: https://www.docker.com/products/docker-desktop/
    pause
    exit /b 1
)

echo Checking Docker service...
docker info >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: Docker service not running
    echo Please start Docker Desktop
    pause
    exit /b 1
)

echo Docker check passed!
echo.

REM Select build method
echo Please select build method for x86 (32-bit) Linux:
echo 1) Docker build (Recommended)
echo 2) WSL build
echo 3) Cross compile
echo 4) Exit

set /p choice="Enter your choice (1-4): "

if "%choice%"=="1" goto docker_build
if "%choice%"=="2" goto wsl_build
if "%choice%"=="3" goto cross_build
if "%choice%"=="4" goto exit
goto invalid_choice

:docker_build
echo.
echo Starting Docker x86 build...
echo.

REM Create Docker build script for x86
echo Creating Docker build script for x86...

(
echo #!/bin/bash
echo set -e
echo.
echo echo "=========================================="
echo echo "Docker x86 Linux Build Script"
echo echo "=========================================="
echo.
echo # Update system
echo echo "Updating system..."
echo sudo apt update
echo.
echo # Install dependencies for x86
echo echo "Installing x86 dependencies..."
echo sudo apt install -y cmake g++ make git qt6-base-dev qt6-tools-dev gcc-multilib g++-multilib
echo.
echo # Enter project directory
echo echo "Entering project directory..."
echo cd /mnt/c/Users/%USERNAME%/Projects/SVN/gitee/sokit
echo.
echo # Create build directory
echo echo "Creating build directory..."
echo mkdir -p build-cli-x86
echo cd build-cli-x86
echo.
echo # Configure CMake for x86
echo echo "Configuring CMake for x86..."
echo cmake ../src/sokit-cli -DCMAKE_BUILD_TYPE=Release -DCMAKE_SYSTEM_PROCESSOR=i686
echo.
echo # Compile
echo echo "Compiling project..."
echo make -j$(nproc)
echo.
echo # Create package
echo echo "Creating package..."
echo mkdir -p sokit-cli-linux-x86
echo cp sokit-cli sokit-cli-linux-x86/
echo cp ../README.md sokit-cli-linux-x86/ 2>/dev/null || true
echo cp ../LINUX_BUILD_GUIDE.md sokit-cli-linux-x86/ 2>/dev/null || true
echo.
echo # Create tar.gz package
echo echo "Packaging..."
echo tar -czf sokit-cli-linux-x86-$(date +%%Y%%m%%d).tar.gz sokit-cli-linux-x86/
echo.
echo echo "=========================================="
echo echo "x86 build completed!"
echo echo "=========================================="
echo echo "Executable: build-cli-x86/sokit-cli"
echo echo "Package: build-cli-x86/sokit-cli-linux-x86-*.tar.gz"
echo echo "=========================================="
) > docker_x86_build.sh

echo Docker build script created: docker_x86_build.sh
echo.

REM Run Docker build
echo Starting Docker x86 build...
echo Note: This may take longer for first run
echo.

docker run --rm -v "$(pwd):/workspace" -v "$(pwd)/docker-output:/output" ubuntu:22.04 bash -c "
    apt update && apt install -y cmake g++ make git qt6-base-dev qt6-tools-dev gcc-multilib g++-multilib &&
    cd /workspace &&
    mkdir -p build-cli-x86 && cd build-cli-x86 &&
    cmake ../src/sokit-cli -DCMAKE_BUILD_TYPE=Release -DCMAKE_SYSTEM_PROCESSOR=i686 &&
    make -j$(nproc) &&
    mkdir -p sokit-cli-linux-x86 &&
    cp sokit-cli sokit-cli-linux-x86/ &&
    tar -czf sokit-cli-linux-x86-$(date +%Y%m%d).tar.gz sokit-cli-linux-x86/ &&
    cp sokit-cli-linux-x86-*.tar.gz /output/
"

if %errorlevel% equ 0 (
    echo.
    echo ==========================================
    echo Docker x86 build completed!
    echo ==========================================
    echo Build results located at: docker-output/
    echo.
    if exist "docker-output" (
        echo Generated files:
        dir docker-output
    )
) else (
    echo.
    echo Docker x86 build failed, please check error messages
)

REM Clean up
del docker_x86_build.sh

pause
exit /b 0

:wsl_build
echo.
echo Starting WSL x86 build...
echo.

REM Check WSL
wsl --version >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: WSL not found
    echo Please install WSL first: wsl --install
    pause
    exit /b 1
)

echo WSL check passed!
echo.

REM Create WSL build script for x86
echo Creating WSL build script for x86...

(
echo #!/bin/bash
echo set -e
echo.
echo echo "=========================================="
echo echo "WSL x86 Linux Build Script"
echo echo "=========================================="
echo.
echo # Update system
echo echo "Updating system..."
echo sudo apt update
echo.
echo # Install x86 dependencies
echo echo "Installing x86 dependencies..."
echo sudo apt install -y cmake g++ make git qt6-base-dev qt6-tools-dev gcc-multilib g++-multilib
echo.
echo # Enter project directory
echo echo "Entering project directory..."
echo cd /mnt/c/Users/%USERNAME%/Projects/SVN/gitee/sokit
echo.
echo # Create build directory
echo echo "Creating build directory..."
echo mkdir -p build-cli-x86
echo cd build-cli-x86
echo.
echo # Configure CMake for x86
echo echo "Configuring CMake for x86..."
echo cmake ../src/sokit-cli -DCMAKE_BUILD_TYPE=Release -DCMAKE_SYSTEM_PROCESSOR=i686
echo.
echo # Compile
echo echo "Compiling project..."
echo make -j$(nproc)
echo.
echo # Create package
echo echo "Creating package..."
echo mkdir -p sokit-cli-linux-x86
echo cp sokit-cli sokit-cli-linux-x86/
echo cp ../README.md sokit-cli-linux-x86/ 2>/dev/null || true
echo cp ../LINUX_BUILD_GUIDE.md sokit-cli-linux-x86/ 2>/dev/null || true
echo.
echo # Create tar.gz package
echo echo "Packaging..."
echo tar -czf sokit-cli-linux-x86-$(date +%%Y%%m%%d).tar.gz sokit-cli-linux-x86/
echo.
echo echo "=========================================="
echo echo "x86 build completed!"
echo echo "=========================================="
echo echo "Executable: build-cli-x86/sokit-cli"
echo echo "Package: build-cli-x86/sokit-cli-linux-x86-*.tar.gz"
echo echo "=========================================="
) > wsl_x86_build.sh

echo WSL build script created: wsl_x86_build.sh
echo.

REM Run WSL build
echo Starting WSL x86 build...
echo Note: First run may take longer to install dependencies
echo.

wsl bash wsl_x86_build.sh

if %errorlevel% equ 0 (
    echo.
    echo ==========================================
    echo WSL x86 build completed!
    echo ==========================================
    echo Build results located at: build-cli-x86/
    echo.
    if exist "build-cli-x86" (
        echo Generated files:
        dir build-cli-x86
    )
) else (
    echo.
    echo WSL x86 build failed, please check error messages
)

REM Clean up
del wsl_x86_build.sh

pause
exit /b 0

:cross_build
echo.
echo Starting cross compile x86 build...
echo.

REM Run cross compile with x86 target
echo Running cross compile for x86...
cross_compile_linux.bat

pause
exit /b 0

:invalid_choice
echo Invalid choice, please run script again
pause
exit /b 1

:exit
echo Exiting x86 build script
pause
exit /b 0 